package portal.mybatis.dao.service;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import portal.mybatis.dao.JobEntity;
import portal.mybatis.dao.mapper.JobEntityMapper;
import portal.mybatis.page.Page;
import utils.Log4jInit;
@Service
public class JobDetailService {
	private static final String[] prop={"jobId","jobName","jobStatus","executeHost"};
	private static final String[] drow={"job_id","job_name","job_status","execute_host"};
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Autowired
	JobEntityMapper jobEntityMapper;
	public List<JobEntity> getPageData(Page<JobEntity> page){
		//System.out.println(jobEntityMapper.searchJobByText(page).size());
		ArrayList<JobEntity> list =jobEntityMapper.selectJobByText(page);
		return list;//return null;
	}
	public Long countJobByText(Page<JobEntity> page){
		return jobEntityMapper.countJobByText(page);//return jobEntityMapper.countByPage();
	}
	public boolean insertJobDetail(String jobId ,String jobName,String status,String jobQueue,
			String priority,String strategy,String command,String execFilePath,Integer slotQua,
			String memeUsed,Integer CpuKerNum ,String startInfo,String execHost,String cpuUsage,
			Integer MPI, String dependFile){
		JobEntity job=new JobEntity();
		job.setJobId(jobId);
		job.setJobStatus(status);
		job.setJobName(jobName);
		job.setJobSequence(jobQueue);
		
		job.setJobPriority(priority);
		job.setScheduleStrategy(strategy);
		job.setCommand(command);
		job.setExecuteFilePath(execFilePath);
		job.setJobSlotQuantity(slotQua);
		
		job.setUsedMemory(memeUsed);
		job.setOccupiedCpuQuantity(CpuKerNum);
		job.setStartInfo(startInfo);
		job.setExecuteHost(execHost);
		job.setCpuUsage(cpuUsage);
		
		job.setMpiProcessorsQuantity(MPI);
		job.setDependentFiles(dependFile);
		return jobEntityMapper.insertSelective(job)>0;
	}
	public Page<JobEntity> getPage(Page<JobEntity> page){
		page.setOrders(pageOrderAnalysis(page));
		page.setRows(getPageData(page));
		page.setTotal(countJobByText(page));		
		Log4jInit.logger.info(page.getOrders());
		return page;
	}
	private String pageOrderAnalysis(Page<JobEntity> page){
		String sort=page.getSort();
		String order=page.getSortOrder();
		if(sort!=null && order!=null){
			for(int i=0;i<prop.length;i++){
				if(prop[i].equals(sort)){
					return drow[i] +" " +order;//sort= drow[i];
				}
			}
		}
		return null;
	}
	public boolean updateJobDeleted(Integer deleted,String jobId){
		return updateScheduleJobDetail(jobId,null,0,null,null,null,null,null,deleted);
	}
	public boolean updateStateById(String status,String jobId){
		return updateScheduleJobDetail(jobId,status,0L,null,null,null,null,null,null);
		//return jobEntityMapper.updateBySelective(job)>0;
	}
	public boolean updateScheduleJobDetail(String jobId, String status, String endTime, String memUsage,
			String cpuTime, String executionHost, String cpuUsage, String remainMem) {
		long endTime1=0;
		try {
			endTime1 = sdf.parse(endTime).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			endTime1=System.currentTimeMillis();
		}
		return updateScheduleJobDetail(jobId,status,endTime1,memUsage,cpuTime,executionHost,cpuUsage,remainMem,null);
	}
	public boolean updateScheduleJobDetail(String jobId, String status, long endTime, String memUsage,
			String cpuTime, String executionHost, String cpuUsage, String remainMem ,Integer deleted) {
		JobEntity job =new JobEntity();
		job.setJobStatus(status);
		job.setJobId(jobId);
		job.setEndTime(new Date(endTime));
		job.setUsedMemory(memUsage);
		job.setCpuExecutedTime(cpuTime);
		job.setExecuteHost(executionHost);
		job.setCpuUsage(cpuUsage);
		job.setRemainMem(remainMem);
		job.setDeleted(deleted);
		return jobEntityMapper.updateBySelective(job)>0;
	}
	public JobEntity selectJobById(String jobId){
		if(jobId==null||jobId.length()==0)
			return null;
		JobEntity job =new JobEntity();
		job.setJobId(jobId);
		List<JobEntity>list =jobEntityMapper.selectJobById(job);
		if(list==null ||list.size()==0)
			return null;
		return list.get(0);
	}
	public List<JobEntity> selectListJobById(String jobId){
		JobEntity job =new JobEntity();
		job.setJobId(jobId);
		return jobEntityMapper.selectJobById(job);
	}
	public Page<JobEntity> selectJobByNode(String execHost, Page<JobEntity> page){
		JobEntity job =new JobEntity();
		job.setExecuteHost(execHost);
		page.setSear(job);
		page.setRows(jobEntityMapper.selectJobByNode(page));
		page.setTotal(jobEntityMapper.countJobByNode(page));
		return page;
	}
}
